package seckill.web;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import seckill.dto.Exposer;
import seckill.dto.SeckillExecution;
import seckill.dto.SeckillResult;
import seckill.entity.Seckill;
import seckill.enums.SeckillStatEnum;
import seckill.exception.RepeatKillException;
import seckill.exception.SeckillCloseException;
import seckill.exception.SeckillException;
import seckill.service.SeckillService;

import org.slf4j.Logger;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/seckill") //url:/模块/资源/{id/细分 /seckill/list
public class SeckillController
{
    private final Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SeckillService seckillService;

    @RequestMapping(value ="/list",method = RequestMethod.GET)
    public String list(Model model)
    {
        //获取列表页
        List<Seckill> list=seckillService.getSeckillList();
        model.addAttribute("list",list);
        //detail.jsp+model=ModelAndView
        return "list";//WEB-INF/jsp/"list".jsp
    }

    @RequestMapping(value="/{seckillId}/detail",method=RequestMethod.GET)
    public String detail(@PathVariable("seckillId") Long seckillId, Model model)
    {
        if(seckillId==null)
        {
            return "redirect:/seckill/list";
        }
        Seckill seckill=seckillService.getById(seckillId);
        if(seckill==null)
        {
            return "forward:/seckill/list";
        }
        model.addAttribute("seckill",seckill);
        return "detail";
    }

    //ajax json
    @RequestMapping(value="/{seckillId}/exposer",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId)
    {
        SeckillResult<Exposer> result;
        try
        {
            Exposer exposer=seckillService.exportSeckillUrl(seckillId);
            result=new SeckillResult<Exposer>(true,exposer);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(),e);
            result=new SeckillResult<Exposer>(false,e.getMessage());
        }
        return result;
    }

    @RequestMapping(value="/{seckillId}/{md5}/execution",
            method=RequestMethod.POST,
            produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId,
                                                   @PathVariable("md5") String md5,
                                                   @CookieValue(value = "userPhone",required=false) Long userPhone)
    {

        //springMVC的验证信息
        if(userPhone == null)
            return new SeckillResult<SeckillExecution>(false,"未注册");
        SeckillResult<SeckillExecution> result;
        try
        {
            logger.error("&&&&&&&&&&&&&&&&&&&&&&&&&&");
            //SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5);
            //调用存储过程 优化
            SeckillExecution execution = seckillService.executeSeckillProcedure(seckillId, userPhone, md5);
            return new SeckillResult<SeckillExecution>(true,execution);
        }
        catch(RepeatKillException e)
        {
            logger.error("@@@@@@@@@@@@@@@@@@@@@@@@@@");
            SeckillExecution execution=new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
            return new SeckillResult<SeckillExecution>(true,execution);
        }
        catch(SeckillCloseException e)
        {
            logger.error("##########################");
            SeckillExecution execution=new SeckillExecution(seckillId, SeckillStatEnum.END);
            return new SeckillResult<SeckillExecution>(true,execution);
        }
        catch(Exception e)
        {
            logger.error("$$$$$$$$$$$$$$$$$$$$$$$$$$$");
            logger.error(e.getMessage(),e);
            SeckillExecution execution=new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
            return new SeckillResult<SeckillExecution>(true,execution);
        }
    }

    @RequestMapping(value="/time/now",method=RequestMethod.GET)
    @ResponseBody
    //seckillResult<T> T Java泛型
    public SeckillResult<Long> time()
    {
        Date now=new Date();
        //SeckillResult封装了所有json请求

        return new SeckillResult(true,now.getTime());
    }

}
